package com.my.blog.myblog.controller.admin;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.blog.annotation.SystemLog;
import com.my.blog.myblog.entity.Bloger;
import com.my.blog.myblog.service.BlogerService;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/1 14:42
 * @Description： 管理端的请求跳转
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminIndexController {

    @Resource
    private BlogerService blogerService;

    //首页
    @GetMapping("/index_v1")
    public ModelAndView indexV1(){
        return new ModelAndView("admin/index_v1");
    }

    //友链
    @GetMapping("/links/links")
    public ModelAndView links(){
        return new ModelAndView("admin/links/links");
    }

    //友链审核
    @GetMapping("/links/isApplyLinks")
    public ModelAndView isApplyLinks(){
        return new ModelAndView("admin/links/isApplyLinks");
    }

    //统计图表
    @GetMapping("/charts/eCharts")
    public ModelAndView eCharts(){
        return new ModelAndView("admin/charts/eCharts");
    }

    //博客管理
    @GetMapping("/blog/blogTable")
    public ModelAndView blogTable(){
        return new ModelAndView("admin/blog/blogTable");
    }

    //写博客
    @GetMapping("/blog/addBlog")
    public ModelAndView addBlog(){
        return new ModelAndView("admin/blog/addBlog1");
    }

    //博客编辑
    @GetMapping("/blog/updateBlog")
    public ModelAndView updateBlog(){
        return new ModelAndView("admin/blog/updateBlog");
    }

    //博客查询
    @GetMapping("/blog/findBlog")
    public ModelAndView findBlog(){
        return new ModelAndView("admin/blog/findBlog");
    }

    //博客类别
    @GetMapping("/blog/blogType")
    public ModelAndView blogType(){
        return new ModelAndView("admin/blog/blogType");
    }

    //资源模块
    @GetMapping("/resource/resource")
    public ModelAndView resource(){
        return new ModelAndView("admin/resource/resource");
    }

    //访客记录
    @GetMapping("/visit/visit")
    public ModelAndView visit(){
        return new ModelAndView("admin/visit/visit");
    }

    //访客统计
    @GetMapping("/visit/visitTable")
    public ModelAndView visitTable(){
        return new ModelAndView("admin/visit/visitTable");
    }

    //黑名单
    @GetMapping("/visit/black")
    public ModelAndView black(){
        return new ModelAndView("admin/visit/black");
    }

    //操作日志
    @GetMapping("/log/log")
    public ModelAndView log(){
        return new ModelAndView("admin/log/log");
    }

    //本地图库
    @GetMapping("/pics/pics")
    public ModelAndView pics(){
        return new ModelAndView("admin/pics/pics");
    }

    //黑名单调转
    @GetMapping("/blackIp")
    public ModelAndView blackIp(){
        return new ModelAndView("/error/blackIp");
    }

    //修改密码
    @GetMapping("/updatePw")
    @SystemLog(description = ConstantUtil.UPDATE_PASSWORD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updatePw(@RequestParam(value = "oldPw")String oldPw,
                                       @RequestParam(value = "newPw")String newPw,
                                       @RequestParam(value = "username")String username){
        ResultBean<Object> resultBean=new ResultBean<>();
        QueryWrapper<Bloger> queryWrapper=new QueryWrapper<>();
        Bloger bloger = blogerService.getOne(queryWrapper.lambda().eq(Bloger::getUsername, username));
        //就没这个用户
        if(ObjectUtil.isEmpty(bloger)){
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("根据用户名未查询到该用户！");
            return resultBean;
        }
        //老密码加密,准备比较
        String oldPassword = SaSecureUtil.md5BySalt(oldPw, bloger.getUsername());
        //原密码不符
        if(!oldPassword.equals(bloger.getPassword())){
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("原密码错误！");
            return resultBean;
        }
        //新密码加密
        String newPassword = SaSecureUtil.md5BySalt(newPw, bloger.getUsername());
        bloger.setPassword(newPassword);
        if(blogerService.updateById(bloger)){
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setMsg("修改成功！");
        }else {
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("出现了未知问题0.0");
        }
        return resultBean;
    }


    /*原shiro的密码修改
    //修改密码
    @GetMapping("/updatePw")
    @SystemLog(description = ConstantUtil.UPDATE_PASSWORD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updatePw(@RequestParam(value = "oldPw")String oldPw,
                                       @RequestParam(value = "newPw")String newPw,
                                       @RequestParam(value = "username")String username){
        ResultBean<Object> resultBean=new ResultBean<>();
        QueryWrapper<Bloger> queryWrapper=new QueryWrapper<>();
        Bloger bloger = blogerService.getOne(queryWrapper.lambda().eq(Bloger::getUsername, username));
        //就没这个用户
        if(ObjectUtil.isEmpty(bloger)){
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("根据用户名未查询到该用户！");
            return resultBean;
        }
        //原密码加密
        //加密方式
        String hashname="MD5";
        //原密码
        Object oldCred=oldPw;
        //盐
        ByteSource bytes = ByteSource.Util.bytes(username);
        //hash迭代次数
        int hashInt=1024;
        SimpleHash oldSimpleHash=new SimpleHash(hashname,oldCred,bytes,hashInt);
        //加密后的密码
        String password=oldSimpleHash.toString();
        //原密码不符 写错了..
        if(!oldPw.equals(password)){
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("原密码错误！");
            return resultBean;
        }
        //新密码加密
        Object newCred=newPw;
        SimpleHash newSimpleHash=new SimpleHash(hashname,newCred,bytes,hashInt);
        bloger.setPassword(newSimpleHash.toString());
        if(blogerService.updateById(bloger)){
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setMsg("修改成功！");
        }else {
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("出现了未知问题0.0");
        }
        return resultBean;
    }*/
}
